
// #ifdef MP-WEIXIN
var COS = require('./cos-wx-sdk-v5');
// #endif
// #ifdef H5 
var COS = require('./cos-wx-sdk');
// #endif
import utils from './utils.js';
var config = {
	Bucket: 'mioumanage-1259471224',
	Region: 'ap-chengdu'
};;

var TaskId;

var cos = new COS({
    getAuthorization: function (options, callback) {
		var authorization = COS.getAuthorization({
			SecretId: 'AKIDHJRJ0TwdJhp30hBHBuOdz4shHacNqXtx', //密钥ID
			SecretKey: 'HI2VorhiDSP6X6UzaKpRnbEWYJUPRntj', //密钥
			Method: options.Method,
			Pathname: options.Pathname,
			Query: options.Query,
			Headers: options.Headers,
			Expires: 60,
		});
		callback({
			Authorization: authorization,
		});
    },
});

// 上传文件
var postObject = function(callback, prefix='imagelist/') {
	uni.chooseImage({
		count: 1, // 默认9
		sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
		sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
		success: function(res) {
			var file = res.tempFiles[0];
			var Key = prefix + utils.getRandFileName(file.name || file.path);
			cos.postObject({
				Bucket: config.Bucket,
				Region: config.Region,
				FilePath: file.path,
				Key,
				TaskReady: function(taskId) {
					TaskId = taskId
				},
				onProgress: function(info) {
					console.log(JSON.stringify(info));
					console.log('正在进度:', info);
				}
			}, (err, data) => {
				console.log(err || data);
				if (err && err.error) {
				    wx.showModal({
				      title: '返回错误',
				      content: '请求失败：' + (err.error.Message || err.error) + '；状态码：' + err.statusCode,
				      showCancel: false
				    });
				  } else if (err) {
				    wx.showModal({
				      title: '返回错误',
				      content: '请求出错：' + err + '；状态码：' + err.statusCode,
				      showCancel: false
				    });
				  } else {
				    uni.showToast({
				        title: '上传成功',
				        icon: 'success',
				        duration: 3000
				    });
					//下获取上传成功后的Url
					cos.getObjectUrl({
						Bucket: config.Bucket,
						Region: config.Region,
						Key: Key,
						Sign: false
					}, function(err, data) {
						console.log(data);
						if(err) return
						callback && callback(data.Url) 
					});
				}
			});
			
			
		}
	})
}

var putPath = function(file, callback) {
	file.prefix = file.prefix ? file.prefix :'imagelist/'
	var Key = file.prefix + utils.getRandFileName(file.name || file.path);
	cos.postObject({
		Bucket: config.Bucket,
		Region: config.Region,
		FilePath: file.path,
		Key,
		TaskReady: function(taskId) {
			TaskId = taskId
		},
		onProgress: function(info) {
			console.log(JSON.stringify(info));
			console.log('正在进度:', info);
		}
	}, (err, data) => {
		if (err && err.error) {
		    wx.showModal({
		      title: '返回错误',
		      content: '请求失败：' + (err.error.Message || err.error) + '；状态码：' + err.statusCode,
		      showCancel: false
		    });
		  } else if (err) {
		    wx.showModal({
		      title: '返回错误',
		      content: '请求出错：' + err + '；状态码：' + err.statusCode,
		      showCancel: false
		    });
		  } else {
		    uni.showToast({
		        title: '上传成功',
		        icon: 'success',
		        duration: 3000
		    });
			//下获取上传成功后的Url
			cos.getObjectUrl({
				Bucket: config.Bucket,
				Region: config.Region,
				Key: Key,
				Sign: false
			}, function(err, data) {
				console.log(data);
				if(err) return
				callback && callback(data.Url) 
			});
		}
	});
}


var deleteObject = function(url) {
	let Key = url.split('myqcloud.com/')[1]
	cos.deleteObject({
		Bucket: config.Bucket, // Bucket 格式：test-1250000000
		Region: config.Region,
		Key
	}, function (err, data) {
		console.log(err || data);
	});
}
module.exports = { 
	postObject,
	putPath,
	deleteObject
};